package com.kaishengit.util;
import java.io.File;  
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;  
  
import com.google.zxing.BarcodeFormat;  
import com.google.zxing.EncodeHintType;  
import com.google.zxing.MultiFormatWriter;  
import com.google.zxing.client.j2se.MatrixToImageWriter;  
import com.google.zxing.common.BitMatrix;  
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;  
  
/** 
 * @blog http://sjsky.iteye.com 
 * @author Michael 
 */  
public class ZxingEncoderHandler {  
  
    /** 
     * 编码  这是把 二维码的二进制流写进本地文件中
     * @param contents 
     * @param width 
     * @param height 
     * @param imgPath 
     */  
    public void encode(String contents, int width, int height, String imgPath) {  
        Hashtable<Object, Object> hints = new Hashtable<Object, Object>();  
        // 指定纠错等级  
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);  
        // 指定编码格式  
        hints.put(EncodeHintType.CHARACTER_SET, "GBK");  
        try {  
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,  
                    BarcodeFormat.QR_CODE, width, height, hints);  
  
            MatrixToImageWriter  
                    .writeToFile(bitMatrix, "png", new File(imgPath));  
  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
    /**
     * 把生成的二维码写进输出流中
     * @param contents
     * @param width
     * @param height
     * @param stream
     */
    public void encodeByStream(String contents, int width, int height, OutputStream stream) {
        Hashtable<Object, Object> hints = new Hashtable<Object, Object>();
        // 指定纠错等级
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        // 指定编码格式
        hints.put(EncodeHintType.CHARACTER_SET, "GBK");
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
                    BarcodeFormat.QR_CODE, width, height, hints);

            MatrixToImageWriter
                    .writeToStream(bitMatrix, "jpg", stream);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 
     * @param imgPath
     * @param content
     */
    public void erweima(String imgPath,String content){
    	File file = new File(imgPath);
        if(!file.exists()){
        	try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        String contents = "dfjkjokfojjo";  
        int width = 300, height = 300;  
        ZxingEncoderHandler handler = new ZxingEncoderHandler();  
        handler.encode(contents, width, height, imgPath); 
    	
    }
  
    /** 
     * @param args 
     */  
   /*public static void main(String[] args) {  
        String imgPath = "d:/test/4.png";  
        File file = new File("d:/test/4.png");
        if(!file.exists()){
        	try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        String contents = "hfhfdsjkjsdklj";  
        int width = 300, height = 300;  
        ZxingEncoderHandler handler = new ZxingEncoderHandler();  
        handler.encode(contents, width, height, imgPath);  
  
        System.out.println("Michael ,you have finished zxing encode.");  
    } */
}  